package com.ml.loan.sys.controller;


import com.ml.loan.common.annotaion.OperLog;
import com.ml.loan.common.model.JsonResult;
import com.ml.loan.common.model.PageResult;
import com.ml.loan.sys.model.SysDict;
import com.ml.loan.sys.service.SysDictService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;

/**
 * <p>
 * 字典表 前端控制器
 * </p>
 *
 * @author milian
 * @since 2022-08-12
 */
@Controller
@RequestMapping("/system/dict")
public class SysDictController {

    @Autowired
    private SysDictService dictService;

    @RequestMapping("/viewlist")
    public String viewlist() {
        return "system/dict/dict.html";
    }

    @ResponseBody
    @RequestMapping("/dictTypes")
    public PageResult dictTypes(Integer page, Integer limit, String dictName) {
        return dictService.dictTypes(page, limit, dictName);
    }

    @ResponseBody
    @RequestMapping("/dictDatas")
    public PageResult<SysDict> list(Integer page, Integer limit, Integer pid, String dictDataName, String dictDataCode) {
        return dictService.dictDatas(page, limit, pid, dictDataName, dictDataCode);
    }

    @ResponseBody
    @RequestMapping("/addDictType")
    @OperLog(module = "SYS", desc = "字典配置-新增字典类型", result = true)
    public JsonResult addDictType(SysDict dict) {
        if (dictService.save(dict)) {
            return JsonResult.ok("添加成功");
        } else {
            return JsonResult.error("添加失败");
        }
    }

    @ResponseBody
    @RequestMapping("/updateDictType")
    @OperLog(module = "SYS", desc = "字典配置-更新字典类型", result = true)
    public JsonResult updateDictType(SysDict dict) {
        SysDict before = dictService.getById(dict);
        if (before == null || before.getPid() != 0) {
            return JsonResult.error("数据错误");
        }
        if (dictService.updateById(dict)) {
            dictService.clearRedisCache(before.getDictCode());
            return JsonResult.ok("修改成功");
        } else {
            return JsonResult.error("修改失败");
        }
    }

    @ResponseBody
    @RequestMapping("/deleteDictType")
    @OperLog(module = "SYS", desc = "字典配置-删除字典类型", result = true)
    public JsonResult deleteDictType(Integer id) {
        if (dictService.deleteDictType(id)) {
            return JsonResult.ok("删除成功");
        } else {
            return JsonResult.error("删除失败");
        }
    }


    @ResponseBody
    @RequestMapping("/addDictData")
    @OperLog(module = "SYS", desc = "字典配置-新增字典项", result = true)
    public JsonResult addDictData(SysDict dict) {
        if (dictService.save(dict)) {
            //清除缓存
            dictService.clearRedisCache(dict.getId());
            return JsonResult.ok("添加成功");
        } else {
            return JsonResult.error("添加失败");
        }
    }

    @ResponseBody
    @RequestMapping("/updateDictData")
    @OperLog(module = "SYS", desc = "字典配置-更新字典项", result = true)
    public JsonResult updateDictData(SysDict dict) {
        SysDict before = dictService.getById(dict);
        if (before == null || before.getPid() == 0) {
            return JsonResult.error("数据错误");
        }
        if (dictService.updateById(dict)) {
            //清除缓存
            dictService.clearRedisCache(dict.getId());
            return JsonResult.ok("修改成功");
        } else {
            return JsonResult.error("修改失败");
        }
    }

    @ResponseBody
    @RequestMapping("/deleteDictData")
    @OperLog(module = "SYS", desc = "字典配置-删除字典项", result = true)
    public JsonResult deleteDictData(Integer id) {
        SysDict before = dictService.getById(id);
        if (before == null) {
            return JsonResult.error("删除的数据不存在");
        }
        if (dictService.removeById(id)) {
            //清除缓存
            dictService.clearRedisCache(dictService.getById(before.getPid()).getDictCode());
            return JsonResult.ok("删除成功");
        } else {
            return JsonResult.error("删除失败");
        }
    }


    @ResponseBody
    @RequestMapping("/batchHandleDictData")
    @OperLog(module = "SYS", desc = "字典配置-批量操作字典项", result = true)
    public JsonResult batchHandleDictData(String ids, Integer state) {
        if (StringUtils.isBlank(ids) || state == null) {
            return JsonResult.error("参数不能为空");
        }
        if (dictService.batchHandleDictData(ids, state)) {
            String[] split = ids.split(",");
            if (split.length > 0) {
                dictService.clearRedisCache(Integer.valueOf(split[0]));
            }
            return JsonResult.ok();
        } else {
            return JsonResult.error();
        }
    }


}
